/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: JdoIndicatorDAO.java
 * Date: 01/03/2011
 */

package com.ingenius.sau.server.persistence.jdo;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.ingenius.sau.server.persistence.IndicatorDAO;
import com.ingenius.sau.shared.model.guidelines.Feature;
import com.ingenius.sau.shared.model.guidelines.Indicator;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>JdoIndicatorDAO</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class JdoIndicatorDAO implements IndicatorDAO {

	private static Logger logger = Logger.getLogger(JdoIndicatorDAO.class
			.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#save(java.lang.Object)
	 */
	@Override
	public Boolean save(Indicator object) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Boolean result = false;
		try {
			logger.info("Saving object...");
			tx.begin();
			String id = object.getFeature().getId();
			object.setFeature(null);
			Feature feature = pm.getObjectById(Feature.class, id);
			feature.addIndicator(object);
			tx.commit();
			result = true;
			logger.fine("ok -> object saved [" + object + "]");

		} catch (Exception e) {
			String msg = "Failed to save indicator [" + object + "]. ";
			logger.log(Level.SEVERE, msg, e);
			System.out.println(msg + e.getMessage());

		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#update(java.lang.Object)
	 */
	@Override
	public Boolean update(Indicator copy) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Boolean result = false;
		try {
			logger.info("Updating object...");
			tx.begin();
			Indicator save = pm.getObjectById(Indicator.class, copy.getId());
			save.setDescription(copy.getDescription());
			save.setName(copy.getName());
			tx.commit();
			result = true;
			logger.fine("ok -> object update [" + save + "]");

		} catch (Exception e) {
			String msg = "Failed to update indicator [" + copy + "]";
			logger.log(Level.SEVERE, msg, e);
			System.out.println(msg);

		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#delete(java.lang.String)
	 */
	@Override
	public Boolean delete(String id) {
		return JdoUtil.delete(Indicator.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#get(java.lang.String)
	 */
	@Override
	public Indicator get(String id) {
		return JdoUtil.get(Indicator.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#count(java.util.List)
	 */
	@Override
	public Integer count(List<Filter> filters) {
		return JdoUtil.count(Indicator.class, filters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#getRange(java.util.List,
	 * java.lang.String, int, int)
	 */
	@Override
	public List<Indicator> getRange(List<Filter> filters, String orderBy,
			int start, int limit) {

		return JdoUtil
				.getRange(Indicator.class, filters, orderBy, start, limit);
	}

}
